package com.edu.chongdian;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.Locale;

public class QiandaoMainActivity extends AppCompatActivity {
    private ImageView signInButton;
    private TextView pointsTextView;
    private TextView textView;
    private int points = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qiandaoactivity_main);
        signInButton=findViewById(R.id.image_view);
        pointsTextView=findViewById(R.id.textview);
        textView=findViewById(R.id.tiaozhuan);
        // 读取保存的积分值，如果没有则默认为0
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        points = prefs.getInt("points", 0);
        pointsTextView.setText(String.format(Locale.getDefault(), "您已获得%d个积分", points));
        // 添加签到按钮的点击事件
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 判断今天是否已经签到过
                if (isTodaySignedIn()) {
                    Toast.makeText(QiandaoMainActivity.this, "您今天已经签到过了！", Toast.LENGTH_SHORT).show();
                } else {
                    // 增加积分值
                    points += 5;
                    // 保存积分值
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putInt("points", points);
                    editor.apply();
                    // 更新显示的积分值
                    pointsTextView.setText(String.format(Locale.getDefault(), "您已获得%d个积分", points));
                    signInButton.setImageResource(R.drawable.sign_next);
                    Toast.makeText(QiandaoMainActivity.this, "签到成功！您已获得5个积分。", Toast.LENGTH_LONG).show();
                }
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(QiandaoMainActivity.this,ShangchengMainActivity.class);
                intent.putExtra("data",points);
                startActivity(intent);
            }
        });

        Log.e("TAG","积分值"+points);
    }
    /**
     * 判断今天是否已经签到过
     */
    private boolean isTodaySignedIn() {
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        // 读取上次签到日期，如果没有则默认为0
        long lastSignInTime = prefs.getLong("last_sign_in_time", 0);
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        int today = calendar.get(Calendar.DAY_OF_YEAR);
        // 如果上次签到日期不等于今天的日期，则说明今天还没有签到
        return lastSignInTime == today;
    }
    /**
     * 更新上次签到日期
     */
    private void updateLastSignInTime() {
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        Calendar calendar = Calendar.getInstance();
        int today = calendar.get(Calendar.DAY_OF_YEAR);
        editor.putLong("last_sign_in_time", today);
        editor.apply();
    }
}